/*
* Snyke
* Description: In-Situ Editing of text paragraphs on click.
* Based on 'Edit-in-Place with Ajax' of 24ways.org
* @params string element, parameters (get)
*/

function init(){
//	editinplace( document.getElementById('eip01'), '');
}

function clearall(){

	for(i=0; i<200; i++){ 
		var els = document.getElementById('keyword'+i+'_editor'); 
		if( els) {
			Element.remove(els.id);
			Element.show('keyword'+i);
//			alert(els.id.substring();
		}	
	}

}

function editinplace(obj, params){
    Element.hide(obj);
    var textarea = '<div id="'+obj.id+'_editor"><textarea class="editkeyword" id="'+obj.id+'_edit" name="'+obj.id+'" rows="1" cols="60">'+obj.innerHTML+'</textarea>';
    var button   = '<div><input class="b10b" id="'+obj.id+'_save" type="button" value="Save" /> <input class="b10b" id="'+obj.id+'_cancel" type="button" value="Cancel" /> <input class="b10b" id="'+obj.id+'_ban" type="button" value="Ban" /></div></div>';
    new Insertion.After(obj, textarea+button);
    Event.observe(obj.id+'_save', 'click', function(){saveChanges(obj, params)}, false);
    Event.observe(obj.id+'_cancel', 'click', function(){cleanUp(obj)}, false);
    Event.observe(obj.id+'_ban', 'click', function(){saveChangesAndBan(obj, params)}, false);

	document.getElementById(obj.id+'_edit').focus(); 
//    Event.observe(obj.id+'_editor', 'mouseout', function(){cleanUp(obj)}, false);
//    Event.observe(obj.id+'_editor', 'blur', function(){alert('ciao')}, false);
}

function saveChanges(obj, params){
    var new_content =  escape($F(obj.id+'_edit'));
    obj.innerHTML   = "<img src='/imgs/loading.gif'> Saving...";
  cleanUp(obj, true);
  
    var success = function(t){editComplete(t, obj);}
    var failure = function(t){editFailed(t, obj);}
  var url = '/ajax/keywords.aspx';
    var pars = '&'+params+'&ajax=editinplace&id='+obj.id+'&banned=0'+'&keyword='+new_content;
    var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function saveChangesAndBan(obj, params){
    var new_content =  escape($F(obj.id+'_edit'));
    obj.innerHTML   = "<img src='/imgs/loading.gif'> Saving...";
  cleanUp(obj, true);
  
    var success = function(t){editCompleteBan(t, obj);}
    var failure = function(t){editFailed(t, obj);}
  var url = '/ajax/keywords.aspx';
    var pars = '&'+params+'&ajax=editinplace&id='+obj.id+'&banned=1'+'&keyword='+new_content;
    var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function cleanUp(obj, keepEditable){
    Element.remove(obj.id+'_editor');
    Element.show(obj);
    //new Effect.Highlight(obj, { duration: 3.0 });
}

function editComplete(t, obj){
	var xmlDocument = t.responseXML;

	obj.innerHTML   = xmlDocument.getElementsByTagName('row')[0].attributes.getNamedItem("keyword").value;

    obj.style.textDecoration = '';

	if( xmlDocument.getElementsByTagName('row')[0].attributes.getNamedItem("whathappen") )
		alert(xmlDocument.getElementsByTagName('row')[0].attributes.getNamedItem("whathappen").value);
}

function editCompleteBan(t, obj){
	var xmlDocument = t.responseXML;

	obj.innerHTML   = xmlDocument.getElementsByTagName('row')[0].attributes.getNamedItem("keyword").value;

    obj.style.textDecoration = 'line-through';

	if( xmlDocument.getElementsByTagName('row')[0].attributes.getNamedItem("whathappen") )
		alert(xmlDocument.getElementsByTagName('row')[0].attributes.getNamedItem("whathappen").value);
}

function editFailed(t, obj){
    obj.innerHTML   = 'Could not save...';
    cleanUp(obj);
}
